#include <iostream>
#include "game.h"

#include "SDL.h"
void game::handleInput(SDL_Event event){
	//Poll event to see if any changes have occurred
	while (SDL_PollEvent(&event))
	{
		if (event.type == SDL_QUIT) {
			//Exit program
			 _keepRunning = false;
		} else if (event.type == SDL_KEYDOWN) {
			//Exit program
			_keepRunning = false;
		} else if (event.type == SDL_MOUSEMOTION) {
			SDL_Point s;
			s.x = event.motion.x;
			s.y = event.motion.y;
			for(int i = 0; i < 10; ++i){
				for(int e = 0; e < 10; ++e){
					collision c = get_collider_from_rect(*(_map.get_vector_map()[i][e].get_rect()));

					if(c.is_point_inside(s)){
						_map.get_vector_map()[i][e].set_texture_id(1);
					}
				}
			}
		}
    }
}
game::game():_keepRunning(true), _map(10){

	SDL_Rect destinationRectangle;
	
	SDL_Event event;

	while(_keepRunning){

		_renderer.render(_map);

		handleInput(event);

		SDL_Delay(10);
		_renderer.clear();
	}
}

game::~game(){ }

collision game::get_collider_from_rect(SDL_Rect rect){
	SDL_Point p1 = {rect.x + (rect.w / 2), rect.y};

	SDL_Point p2 = {rect.x + rect.w, rect.y + (rect.h / 2)};

	SDL_Point p3 = {rect.x + (rect.w / 2), rect.y + rect.h};

	SDL_Point p4 = {rect.x, rect.y + (rect.h / 2)};

	collision c(p1, p2, p3, p4);
	return c;
}


